package com.weiaibenpao.demo.chislim.db.preferences;

import android.content.Context;
import android.content.SharedPreferences;

import com.orhanobut.logger.Logger;
import com.weiaibenpao.demo.chislim.MyApplication;
import com.weiaibenpao.demo.chislim.bean.UserBean;

public class UserPreferences {
    public static final String IS_LOGGED_IN = "is_logged_in";
    private static final String USER_PREFERENCE_NAME = "user_info";
    public static final String UNREAD_MSG_COUNT ="unread_msg_count";
    public static final String USER_ID ="user_id";
    public static final String USER_NAME ="user_name";
    public static final String USER_AVATAR ="user_avatar";   //用户头像
    public static final String USER_IMG_BG="user_img_bg";
    public static final String USER_SEX ="user_sex";
    public static final String USER_BIRTH ="user_birth";
    public static final String USER_HEIGHT ="user_height";
    public static final String USER_WEIGHT ="user_weight";
    public static final String USER_PHONE ="user_phone";
    public static final String USER_INTRO = "user_intro";
    public static final String USER_OPENID = "user_openid";
    public static final String USER_SOURCE = "user_source";
    public static final String IS_SHOW_WHITE_LIST_NOTICE ="is_show_white_list_notice";
    public static final String IS_SHOW_ADD_TO_WHITE_LIST_NOTICE ="is_show_add_to_white_list_notice";

    private SharedPreferences mPref;

    private static class SingletonHolder {
        private static final UserPreferences sInstance = new UserPreferences();
    }

    private UserPreferences(){
        mPref = MyApplication.getInstance().getSharedPreferences(USER_PREFERENCE_NAME, Context.MODE_PRIVATE);
    }

    public static UserPreferences getInstance(){
        return SingletonHolder.sInstance;
    }

    public void setUnreadMsgCount(int unreadMsgCount){
        SharedPreferences.Editor editor = mPref.edit();
        editor.putInt(UNREAD_MSG_COUNT,unreadMsgCount).apply();
    }

    public int getUnreadMsgCount(){
        return  mPref.getInt(UNREAD_MSG_COUNT,0);
    }

    public void putBoolean(String key,boolean boolValue){
        SharedPreferences.Editor editor = mPref.edit();
        editor.putBoolean(key,boolValue).apply();
    }

    public boolean getBoolean(String key){
        return mPref.getBoolean(key,false);
    }


    public void putString(String key ,String strValue){
        SharedPreferences.Editor editor = mPref.edit();
        editor.putString(key, strValue).apply();
    }

    public String getString(String key){
        return mPref.getString(key,"");
    }

    public void putInt(String key ,int intValue){
        SharedPreferences.Editor editor = mPref.edit();
        editor.putInt(key,intValue).apply();
    }

    public int getInt(String key){
        return mPref.getInt(key,0);
    }

    public void putLong(String key,long longValue){
        SharedPreferences.Editor editor = mPref.edit();
        editor.putLong(key,longValue).apply();
    }

    public long getLong(String key){
        return mPref.getLong(key,0);
    }

    public void putFloat(String key,float floatValue){
        SharedPreferences.Editor editor = mPref.edit();
        editor.putFloat(key,floatValue).apply();
    }

    public float getFloat(String key){
        return mPref.getFloat(key,0f);
    }

    /**
     * 保存用户信息
     * @param userDataBean
     */
    public  void putUserData(UserBean userDataBean,boolean isLoadIn) {
       putInt(USER_ID, userDataBean.getId());
       putString(USER_NAME, userDataBean.getUserName());
       putString(USER_AVATAR, userDataBean.getUserImage());
       putString(USER_SEX, userDataBean.getUserSex());
       putString(USER_BIRTH, userDataBean.getUserBirth());
       putInt(USER_HEIGHT, userDataBean.getUserHeigh());
       putFloat(USER_WEIGHT, userDataBean.getUserWeight());
        putString(USER_PHONE, userDataBean.getUserPhone());
       putString(USER_INTRO, userDataBean.getUserIntru());
        putBoolean(IS_LOGGED_IN,isLoadIn);
//        putString(USER_STATE, "1");
        Logger.e("SharedPreferences=="+ UserPreferences.getInstance().getInt(USER_ID));
    }
    //以下两个关于白名单的没有使用putBoolean 和getBoolean是因为它们的默认返回值是true与上面的默认返回false不一样
    public void setIsShowWhiteListNotice(boolean isShow){
        SharedPreferences.Editor editor = mPref.edit();
        editor.putBoolean(IS_SHOW_WHITE_LIST_NOTICE,isShow).apply();
    }

    public boolean getIsShowWhiteListNotice(){
        return mPref.getBoolean(IS_SHOW_WHITE_LIST_NOTICE,true);
    }

    public void setIsShowAddToWhiteListNotice(boolean isShow){
        SharedPreferences.Editor editor = mPref.edit();
        editor.putBoolean(IS_SHOW_ADD_TO_WHITE_LIST_NOTICE,isShow).apply();
    }

    public boolean getIsShowAddToWhiteListNotice(){
        return mPref.getBoolean(IS_SHOW_ADD_TO_WHITE_LIST_NOTICE,true);
    }


    public void clear() {
        mPref.edit().clear().apply();
    }

}
